home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / WINGs / wapplication.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-09  |  3.7 KB  |  189 lines

  1.  
  2.  
  3. #include <unistd.h>
  4.  
  5. #include "WINGsP.h"
  6.  
  7.  
  8. extern void W_InitNotificationCenter(void);
  9.  
  10.  
  11. struct W_Application WMApplication;
  12.  
  13.  
  14. char *_WINGS_progname = NULL;
  15.  
  16.  
  17.  
  18. Bool 
  19. W_ApplicationInitialized(void)
  20. {
  21.     return _WINGS_progname!=NULL;
  22. }
  23.  
  24.  
  25. void
  26. WMInitializeApplication(char *applicationName, int *argc, char **argv)
  27. {
  28.     int i;
  29.     
  30.     assert(argc!=NULL);
  31.     assert(argv!=NULL);
  32.     assert(applicationName!=NULL);
  33.  
  34.     _WINGS_progname = argv[0];
  35.     
  36.     WMApplication.applicationName = wstrdup(applicationName);
  37.     WMApplication.argc = *argc;
  38.  
  39.     WMApplication.argv = wmalloc((*argc+1)*sizeof(char*));
  40.     for (i=0; i<*argc; i++) {
  41.     WMApplication.argv[i] = wstrdup(argv[i]);
  42.     }
  43.     WMApplication.argv[i] = NULL;
  44.     
  45.     /* initialize notification center */
  46.     W_InitNotificationCenter();
  47. }
  48.  
  49.  
  50. void
  51. WMSetResourcePath(char *path)
  52. {
  53.     if (WMApplication.resourcePath)
  54.     wfree(WMApplication.resourcePath);
  55.     WMApplication.resourcePath = wstrdup(path);
  56. }
  57.  
  58.  
  59. char*
  60. WMGetApplicationName()
  61. {
  62.     return WMApplication.applicationName;
  63. }
  64.  
  65.  
  66. static char*
  67. checkFile(char *path, char *folder, char *ext, char *resource)
  68. {
  69.     char *ret;
  70.     int extralen;
  71.     
  72.     extralen = (ext ? strlen(ext) : 0) + (folder ? strlen(folder) : 0) + 4; 
  73.     ret = wmalloc(strlen(path)+strlen(resource)+extralen+8);
  74.     strcpy(ret, path);
  75.     if (folder) {
  76.     strcat(ret, "/");
  77.     strcat(ret, folder);
  78.     }
  79.     if (ext) {
  80.     strcat(ret, "/");
  81.     strcat(ret, ext);
  82.     }
  83.     strcat(ret, "/");
  84.     strcat(ret, resource);
  85.     
  86.     if (access(ret, F_OK)!=0) {
  87.     wfree(ret);
  88.     ret = NULL;
  89.     }
  90.  
  91.     return ret;
  92. }
  93.  
  94.  
  95. char*
  96. WMPathForResourceOfType(char *resource, char *ext)
  97. {
  98.     char *path = NULL;
  99.     char *tmp, *appdir;
  100.     int i;
  101.     
  102.     /* 
  103.      * Paths are searched in this order:
  104.      * - resourcePath/ext
  105.      * - argv[0]/ext
  106.      * - GNUSTEP_USER_ROOT/Apps/ApplicationName.app/ext 
  107.      * - ~/GNUstep/Apps/ApplicationName.app/ext
  108.      * - GNUSTEP_LOCAL_ROOT/Apps/ApplicationName.app/ext
  109.      * - /usr/local/GNUstep/Apps/ApplicationName.app/ext
  110.      * - GNUSTEP_SYSTEM_ROOT/Apps/ApplicationName.app/ext
  111.      * - /usr/GNUstep/Apps/ApplicationName.app/ext
  112.      */
  113.     
  114.     if (WMApplication.resourcePath) {
  115.     path = checkFile(WMApplication.resourcePath, NULL, ext, resource);
  116.     if (path)
  117.         return path;
  118.     }
  119.  
  120.     if (WMApplication.argv[0]) {
  121.     tmp = wstrdup(WMApplication.argv[0]);
  122.     i = strlen(tmp);
  123.     while (i > 0 && tmp[i]!='/')
  124.         i--;
  125.     tmp[i] = 0;
  126.     if (i>0) {
  127.         path = checkFile(tmp, NULL, ext, resource);
  128.     } else {
  129.         path = NULL;
  130.     }
  131.     wfree(tmp);
  132.     if (path)
  133.         return path;
  134.     }
  135.     
  136.     appdir = wmalloc(strlen(WMApplication.applicationName)+10);
  137.     sprintf(appdir, "Apps/%s.app", WMApplication.applicationName);
  138.  
  139.     if (getenv("GNUSTEP_USER_ROOT")) {
  140.     path = checkFile(getenv("GNUSTEP_USER_ROOT"), appdir, ext, resource);
  141.     if (path) {
  142.         wfree(appdir);
  143.         return path;
  144.     }
  145.     }
  146.     
  147.     tmp = wusergnusteppath();
  148.     if (tmp) {
  149.     path = checkFile(tmp, appdir, ext, resource);
  150.     if (path) {
  151.         wfree(appdir);
  152.         return path;
  153.     }
  154.     }
  155.     
  156.     if (getenv("GNUSTEP_LOCAL_ROOT")) {
  157.     path = checkFile(getenv("GNUSTEP_LOCAL_ROOT"), appdir, ext, resource);
  158.     if (path) {
  159.         wfree(appdir);
  160.         return path;
  161.     }
  162.     }
  163.     
  164.     path = checkFile("/usr/local/GNUstep", appdir, ext, resource);
  165.     if (path) {
  166.     wfree(appdir);
  167.     return path;
  168.     }
  169.  
  170.     
  171.     if (getenv("GNUSTEP_SYSTEM_ROOT")) {
  172.     path = checkFile(getenv("GNUSTEP_SYSTEM_ROOT"), appdir, ext, resource);
  173.     if (path) {
  174.         wfree(appdir);
  175.         return path;
  176.     }
  177.     }
  178.  
  179.     path = checkFile("/usr/GNUstep", appdir, ext, resource);
  180.     if (path) {
  181.     wfree(appdir);
  182.     return path;
  183.     }
  184.     
  185.     return NULL;
  186. }
  187.  
  188.  
  189.